﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreDemo.Model;
using CoreDome.Services;
using CoreDome.Settings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace CoreDome.Controllers
{
    public class HomeController : Controller
    {
        private readonly ICinemaService cinemaService;
        private readonly IOptions<ConfigOptions> options;

        public HomeController(ICinemaService cinemaService,IOptions<ConfigOptions> options)
        {
            this.cinemaService = cinemaService;
            this.options = options;
        }

        public async Task<IActionResult> Index()
        {
            var list = await cinemaService.GetAllAsync();
            return View(list);
        }

        public  IActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(Cinema cinema)
        {
            if (ModelState.IsValid)
            {
                await cinemaService.AddAsync(cinema);
            }

            return RedirectToAction(nameof(Index));
        }
        
        public async Task<IActionResult> Edit(int id)
        {
            var model = await cinemaService.GetByIdAsync(id);
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(Cinema cinema)
        {
            if (ModelState.IsValid)
            {
                var model = await cinemaService.GetByIdAsync(cinema.Id);
                if (model != null)
                {
                    model.Name = cinema.Name;
                    model.Location = cinema.Location;
                    model.Capacity = cinema.Capacity;

                }
                else
                {
                    return NotFound();
                }
            }
            return RedirectToAction(nameof(Index));
        }
    }
}